﻿using System;
using System.Net;
using System.Collections.Generic;
using System.IO;
using System.Collections;
using System.Text;

namespace Cn.Soncy.Taotao
{
    /*
     * --------------------------------
     * FileName: TaotaoResponse.cs
     * Description: 滔滔响应类,用于返回滔滔数据
     * 
     * Author: Soncy
     * DateTime: 2008-11-27
     * E-mail: soncy@163.com
     * --------------------------------
     */

    /// <summary>
    /// 滔滔响应类,用于返回滔滔数据
    /// </summary>
    public class TaotaoResponse
    {
        /// <summary>
        /// 获取滔滔数据
        /// </summary>
        /// <param name="request">滔滔请求类</param>
        /// <returns>返回滔滔响应类</returns>
        public TaotaoResponse Get(string QQ, int Num, int T)
        {
            /*
             * 使用WebClient返回滔滔的数据并转换为Json
             */

            #region 滔滔类型: QQ号码以及好友滔滔, 设置返回滔滔条目为20.

            if (T == 1)
            {
                Num = 20;
            }

            #endregion

            WebClient client = new WebClient();
            client.Encoding = System.Text.Encoding.UTF8;
            StringBuilder resultBuilder = new StringBuilder(client.DownloadString(new Uri(String.Format("http://www.taotao.com/cgi-bin/msgj?qq={0}&num={1}&t={2}", QQ, Num, T))));
            string strResult = resultBuilder.Remove(0, 6).Remove(resultBuilder.Length - 1, 1).ToString();

            //使用Json.NET转换String为TaotaoResponse类的实例
            TaotaoResponse response = (TaotaoResponse)Newtonsoft.Json.JavaScriptConvert.DeserializeObject(strResult, typeof(TaotaoResponse));

            #region 滔滔类型: QQ号码以及好友滔滔, 在20条数据里面提取每个好友的一条组成数据

            if (T == 1)
            {
                Hashtable qqList = new Hashtable();
                List<TaotaoPost> postList = new List<TaotaoPost>();

                foreach (TaotaoPost post in response.posts)
                {
                    if (qqList.Count <= Num && !qqList.Contains(post.nm))
                    {
                        postList.Add(post);
                        qqList.Add(post.nm, post.nm);
                    }
                }

                response.posts = postList;
            }

            #endregion

            return response;
        }

        /// <summary>
        /// 返回 Json 格式数据
        /// </summary>
        /// <param name="request">Taotao Request 对象</param>
        /// <returns></returns>
        public string ToJson(string QQ, int Num, int T)
        {
            WebClient client = new WebClient();
            client.Encoding = System.Text.Encoding.UTF8;
            StringBuilder resultBuilder = new StringBuilder(client.DownloadString(new Uri(String.Format("http://www.taotao.com/cgi-bin/msgj?qq={0}&num={1}&t={2}", QQ, Num, T))));
            string strResult = resultBuilder.Remove(0, 6).Remove(resultBuilder.Length - 1, 1).ToString();

            return strResult;
        }

        /// <summary>
        /// 返回 Json 格式数据
        /// </summary>
        /// <param name="response">Taotao Response 对象</param>
        /// <returns></returns>
        public string ToJson(TaotaoResponse response)
        {
            return Newtonsoft.Json.JavaScriptConvert.SerializeObject(response);
        }

        /// <summary>
        /// 被查看的滔滔用户信息
        /// </summary>
        public TaotaoUser ui { get; set; }

        /// <summary>
        /// 返回结果状态, 0-成功, 1-失败:可能是对方设置限好友查看或者未开通滔滔
        /// </summary>
        public int ret { get; set; }

        /// <summary>
        /// 所发表的滔滔总数量
        /// </summary>
        public int total { get; set; }

        /// <summary>
        /// 所发表的滔滔列表
        /// </summary>
        public List<TaotaoPost> posts { set; get; }

        /// <summary>
        /// 未知属性
        /// </summary>
        public string rec { get; set; }

        /// <summary>
        /// 未知属性
        /// </summary>
        public int type { get; set; }

        /// <summary>
        /// 滔滔排名
        /// </summary>
        public string rank { get; set; }

        /// <summary>
        /// 返回滔滔用户头像地址
        /// </summary>
        public string headImage
        {
            get
            {
                string basePicture = "http://h.taotao.com/images/head/{1}/{2}/{3}/{4}/{0}.jpg";
                string qq = ui.qq.ToString("000000000");

                return String.Format(basePicture, ui.qq.ToString(), qq.Substring(1, 2), qq.Substring(3, 2), qq.Substring(5, 2), qq.Substring(7, 2));
            }
        }
    }
}
